package listdemo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

public class Test {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        System.out.println(list);
        System.out.println("=========for循环遍历=========");
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) +" ");
        }
        System.out.println();
        System.out.println("=========foreach循环遍历=========");
        for(Integer x : list) {
            System.out.print(x +" ");
        }
        System.out.println();
        System.out.println("=========迭代器遍历1=========");
        Iterator<Integer> it = list.iterator();
        while(it.hasNext()) {
            System.out.print(it.next() +" ");
        }
        System.out.println();
        System.out.println("=========迭代器遍历2=========");
        ListIterator<Integer> tmpList = list.listIterator();
        while(tmpList.hasNext()) {
            System.out.print(tmpList.next() +" ");
        }
        System.out.println();
        System.out.println("=========迭代器遍历3=========");
        ListIterator<Integer> tmpList2 = list.listIterator(list.size());
        while(tmpList2.hasPrevious()) {
            System.out.print(tmpList2.previous() +" ");
        }
        System.out.println();
    }
    public static void main3(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.remove(1);
        System.out.println(list);
        //list.remove(new Integer(1));//已过时，但还能用
        System.out.println(list);
    }
    public static void main2(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        ArrayList<Integer> list2 = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list2.add(11);
        list2.add(22);
        list.addAll(list2);
        System.out.println(list);
        list.set(1,6);
        System.out.println(list);


    }
    public static void main1(String[] args) {
        MyArrayList myArrayList = new MyArrayList();
        myArrayList.add(1);
        myArrayList.add(2);
        myArrayList.add(3);
        myArrayList.add(1,19);
        myArrayList.display();
        myArrayList.set(0,100);
        myArrayList.display();
        boolean ret = myArrayList.contains(100);
        System.out.println(ret);
        myArrayList.set(1,33);
        myArrayList.display();
        myArrayList.add(0,1);
        myArrayList.add(1,2);
        myArrayList.add(2,1);
        myArrayList.add(3,4);
        myArrayList.add(4,1);
        myArrayList.display();
        myArrayList.removeAll(1);
        myArrayList.display();

    }
}
